public class Search {

/*
    33. 搜索旋转排序数组
    将数组一分为二，一部分为有序的，另一部分为无序的，先判断左边有序还是右边有序，
    再判断target在不在有序的那一部分里，根据判断结果来移动边界
 */

    public static void main(String[] args) {
        int[] nums = {4, 5};
        System.out.println(search(nums, 0));
    }

    public static int search(int[] nums, int target) {
        if (nums.length == 1) return nums[0] == target ? 0 : -1;
        if (nums.length == 0) return -1;
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] >= nums[0]) {
                if (nums[0] <= target && nums[mid] > target) {
                    right = mid - 1;
                } else left = mid + 1;
            } else {
                if (nums[mid] <= target && nums[nums.length - 1] >= target) {
                    left = mid + 1;
                } else right = mid - 1;
            }
        }
        return -1;
    }
}
